﻿using System.Application;
using System.Collections.Generic;
using System.Domain.Entity;
using System.Linq;
using Domain.Infrastructure;
using Microsoft.AspNetCore.Mvc;
using Ubiquity.Core.Extend;
using Ubiquity.Core.Web;
using Ubiquity.Core.Web.Tree;
using Ubiquity.Core.Web.TreeGrid;
using Ubiquity.Web.Filters;

namespace Ubiquity.Web.Areas.SystemManage.Controllers
{
    /// <summary>
    ///系统模块 控制器 
    /// </summary>	

    [Area("SystemManage")]
    public class SysModuleController : BaseController
    {
        private readonly SysModuleService _service;

        public SysModuleController(SysModuleService service)
        { 
            _service = service;
        }

        [HttpGet] 
        public ActionResult GetTreeSelectJson()
        {
            var data = _service.GetList().OrderBy(t => t.SortCode);
            var treeList = new List<TreeSelectModel>();
            foreach (SysModuleEntity item in data)
            {
                TreeSelectModel treeModel = new TreeSelectModel
                {
                    id = item.Id,
                    text = item.FullName,
                    parentId = item.ParentId
                };
                treeList.Add(treeModel);
            }
            return Content(treeList.TreeSelectJson());
        }

        [HttpGet]
        [Authorize] 
        public ActionResult GetTreeGridJson(Pagination pagination, string keyword)
        {
            var data = _service.GetList(pagination);
            if (!string.IsNullOrEmpty(keyword))
            {
                data = data.TreeWhere(t => t.FullName.Contains(keyword));
            }
            var treeList = new List<TreeGridModel>();
            foreach (SysModuleEntity item in data)
            {
                TreeGridModel treeModel = new TreeGridModel();
                bool hasChildren = data.Count(t => t.ParentId == item.Id) != 0;
                treeModel.id = item.Id;
                treeModel.isLeaf = hasChildren;
                treeModel.parentId = item.ParentId;
                treeModel.expanded = hasChildren;
                treeModel.entityJson = item.ToJson();
                treeList.Add(treeModel);
            }
            return Content(treeList.TreeGridJson());
        }
        //[HttpGet]
        //[HandlerAjaxOnly]
        //public ActionResult GetGridJson()
        //{
        //    var data = _app.GetList();
        //    return Content(data.ToJson());
        //}
        [HttpGet] 
        public ActionResult GetFormJson(string keyValue)
        {
            var data = _service.GetEntity(keyValue);
            return Content(data.ToJson());
        }
        [HttpPost] 
        [Authorize]
        [ValidateAntiForgeryToken]
        public ActionResult Insert(SysModuleEntity entity, string keyValue)
        {
            return _service.Insert(entity) ? Success("操作成功。") : Error("操作失败。");
        }

        [HttpPost] 
        [Authorize]
        [ValidateAntiForgeryToken]
        public ActionResult Update(SysModuleEntity entity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.Id = keyValue;
                return _service.Update(entity) ? Success("操作成功。") : Error("操作失败。");
            }
            return Error("主键验证错误");
        }
        [HttpPost] 
        [Authorize]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            if (_service.Delete(keyValue))
            {
                return Success("操作成功。");
            }
            else
            {
                return Success("操作失败。");
            }
        }
    }

}
